{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using the Raw API\n",
    "\n",
    "This notebook walks through how to use the raw API to parse documents.\n",
    "\n",
    "Status:\n",
    "| Last Executed | Version | State      |\n",
    "|---------------|---------|------------|\n",
    "| Aug-18-2025   | N/A     | Maintained |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!wget \"https://arxiv.org/pdf/1706.03762.pdf\" -O \"./attention.pdf\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_key = \"llx-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mimetypes\n",
    "import requests\n",
    "import time\n",
    "\n",
    "headers = {\"Authorization\": f\"Bearer {api_key}\"}\n",
    "file_path = \"./attention.pdf\"\n",
    "base_url = \"https://api.cloud.llamaindex.ai/api/parsing\"\n",
    "\n",
    "with open(file_path, \"rb\") as f:\n",
    "    mime_type = mimetypes.guess_type(file_path)[0]\n",
    "    files = {\"file\": (f.name, f, mime_type)}\n",
    "    body = {\n",
    "        \"parse_mode\": \"parse_page_with_agent\",\n",
    "        \"model\": \"openai-gpt-4-1-mini\",\n",
    "        \"high_res_ocr\": True,\n",
    "        \"adaptive_long_table\": True,\n",
    "        \"outlined_table_extraction\": True,\n",
    "        \"output_tables_as_HTML\": True,\n",
    "    }\n",
    "\n",
    "    # send the request, upload the file\n",
    "    url = f\"{base_url}/upload\"\n",
    "    response = requests.post(url, headers=headers, files=files, data=body)\n",
    "\n",
    "response.raise_for_status()\n",
    "# get the job id for the result_url\n",
    "job_id = response.json()[\"id\"]\n",
    "result_type = \"json\"  # or \"markdown\" or \"json\"\n",
    "result_url = f\"{base_url}/job/{job_id}/result/{result_type}\"\n",
    "\n",
    "# check for the result until its ready\n",
    "while True:\n",
    "    response = requests.get(result_url, headers=headers)\n",
    "    if response.status_code == 200:\n",
    "        break\n",
    "\n",
    "    time.sleep(2)\n",
    "\n",
    "# download the result\n",
    "result = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['pages', 'job_metadata'])\n"
     ]
    }
   ],
   "source": [
    "print(result.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['page', 'text', 'md', 'images', 'charts', 'items', 'status', 'originalOrientationAngle', 'links', 'width', 'height', 'triggeredAutoMode', 'parsingMode', 'structuredData', 'noStructuredContent', 'noTextContent', 'pageHeaderMarkdown', 'pageFooterMarkdown', 'confidence'])\n"
     ]
    }
   ],
   "source": [
    "print(result[\"pages\"][0].keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Provided proper attribution is provided, Google hereby grants permission to reproduce the tables and figures in this paper solely for use in journalistic or scholarly works.\n",
      "\n",
      "# Attention Is All You Need\n",
      "\n",
      "**Ashish Vaswani***  \n",
      "Google Brain  \n",
      "avaswani@google.com  \n",
      "\n",
      "**Noam Shazeer***  \n",
      "Google Brain  \n",
      "noam@google.com  \n",
      "\n",
      "**Niki Parmar***  \n",
      "Google Research  \n",
      "nikip@google.com  \n",
      "\n",
      "**Jakob Uszkoreit***  \n",
      "Google Research  \n",
      "usz@google.com  \n",
      "\n",
      "**Llion Jones***  \n",
      "Google Research  \n",
      "llion@google.com  \n",
      "\n",
      "**Aidan N. Gomez* †**  \n",
      "University of Toronto  \n",
      "aidan@cs.toronto.edu  \n",
      "\n",
      "**Łukasz Kaiser***  \n",
      "Google Brain  \n",
      "lukaszkaiser@google.com  \n",
      "\n",
      "**Illia Polosukhin* ‡**  \n",
      "illia.polosukhin@gmail.com  \n",
      "\n",
      "## Abstract\n",
      "\n",
      "The dominant sequence transduction models are based on complex recurrent or convolutional neural networks that include an encoder and a decoder. The best performing models also connect the encoder and decoder through an attention mechanism. We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two machine translation tasks show these models to be superior in quality while being more parallelizable and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task, improving over the existing best results, including ensembles, by over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training costs of the best models from the literature. We show that the Transformer generalizes well to other tasks by applying it successfully to English constituency parsing both with large and limited training data.\n",
      "\n",
      "----\n",
      "\n",
      "*Equal contribution. Listing order is random. Jakob proposed replacing RNNs with self-attention and started the effort to evaluate this idea. Ashish, with Il\n"
     ]
    }
   ],
   "source": [
    "print(result[\"pages\"][0][\"md\"][:2000])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
